package com.haoma.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoma.entity.Photo;
import com.haoma.entity.PhotoCategoryRelation;
import com.haoma.exception.BlogException;
import com.haoma.mapper.PhotoCategoryRelationMapper;
import com.haoma.mapper.PhotoMapper;
import com.haoma.service.PhotoService;
import com.haoma.vo.PhotoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Description:
 */
@Service
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {

    @Autowired
    private PhotoMapper photoMapper;

    @Autowired
    private PhotoCategoryRelationMapper photoCategoryRelationMapper;

    /**
     * 获取分类下的图片
     *
     * @param id
     * @return
     */
    public List<PhotoVo> getPhotoById(Integer id) {
        // 定义一个空集合用于存储
        List<Photo> photoList = new ArrayList<>();
        if (id == 1) {
            LambdaQueryWrapper<PhotoCategoryRelation> photoCategoryRelationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 查询关系表中所有的
            List<PhotoCategoryRelation> photoCategoryRelations = photoCategoryRelationMapper.selectList(photoCategoryRelationLambdaQueryWrapper);
            List<PhotoCategoryRelation> list = photoCategoryRelations.stream().filter(item -> item.getCategoryId() != 7).toList();
            List<Integer> list1 = list.stream().map(PhotoCategoryRelation::getPhotoId).toList();
            List<Photo> photoList1 = photoMapper.selectBatchIds(list1);
            photoList = photoList1.stream().filter(item -> item.getState() == 2).toList();
        } else {
            // 根据分类id查询关系表
            LambdaQueryWrapper<PhotoCategoryRelation> photoCategoryRelationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            photoCategoryRelationLambdaQueryWrapper.eq(PhotoCategoryRelation::getCategoryId, id);
            // 关系表
            List<PhotoCategoryRelation> photoCategoryRelations = photoCategoryRelationMapper.selectList(photoCategoryRelationLambdaQueryWrapper);

            // 校验
            if (CollectionUtils.isEmpty(photoCategoryRelations)) {
                throw new BlogException("该分类下还没有相册图片");
            }

            // 然后根据关系表获取到id
            List<Integer> photoIdList = photoCategoryRelations.stream()
                    .map(PhotoCategoryRelation::getPhotoId).toList();

            // 根据id集合批量查询图片
            photoList = photoMapper.selectBatchIds(photoIdList);
        }


        // 这里需要校验
        if (CollectionUtils.isEmpty(photoList)) {
            throw new BlogException("该分类下还没有相册图片");
        }

        // 过滤到状态为显示2的相册数据
        List<Photo> list = photoList
                // 使用stream流
                .stream()
                // 过滤到状态为2
                .filter(item -> item.getState() == 2)
                // 排序
                .sorted(Comparator.comparing(Photo::getCreateTime))
                .toList();

        // 获取到日期时间
        Set<PhotoVo> dateSet = photoList.stream()
                // 获取到相册状态为显示状态2
                .filter(item -> item.getState() == 2)
                // 排序
                .map(photo -> {
                    // 封装返回对象
                    PhotoVo photoVo = new PhotoVo();
                    // 使用DateTimeFormatter格式化日期为"MM-dd"格式
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
                    // 格式化
                    String format = photo.getCreateTime().format(formatter);
                    // 这里需要判断
                    // 存储起来
                    photoVo.setTime(format);
                    photoVo.setPhotoUrlList(new ArrayList<>());
                    return photoVo;
                }).collect(Collectors.toSet());

        System.out.println(dateSet);
        // 日期排序
        List<PhotoVo> dateList = dateSet
                .stream()
                .sorted(Comparator.comparing(PhotoVo::getTime).reversed())
                .toList();

        // 收集图片数据
        list.forEach(photo -> {
            // 使用DateTimeFormatter格式化日期为"MM-dd"格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
            // 格式化
            String format = photo.getCreateTime().format(formatter);
            // 遍历日期
            for (PhotoVo photoVo : dateList) {
                // 判断
                if (format.equals(photoVo.getTime())) {
                    // 将图片存放起来
                    photoVo.getPhotoUrlList().add(photo.getPhotoUrl());
                }
            }
        });
        return dateList;
    }
}
